package com.leo.action;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.leo.business.AdminBusiness;
import com.leo.entity.User;
import com.leo.formbean.AdminForm;
import com.leo.util.JsonUtil;
import com.leo.vo.Result;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class AdminAction extends ActionSupport implements ModelDriven<AdminForm> {
	@Resource
	private AdminBusiness adminBusiness;
	private AdminForm adminForm = new AdminForm();
	public void addUser(){
		Result result = null;
		try {
			String json = ServletActionContext.getRequest().getParameter("data");
			User user = JsonUtil.toBean(json,User.class);
			result = adminBusiness.userAdd(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result = Result.unknownError();
		}finally{
			JsonUtil.output(result);
		}
	}

	public void deleteUsers() {
		Result result = null;
		try {
			adminBusiness.deleteUsers(adminForm.getIds());
			result = Result.success();
		} catch (Exception e) {
			//e.printStackTrace();
			result = Result.unknownError();
		} finally {
			JsonUtil.output(result);
		}
	}
	@Override
	public AdminForm getModel() {
		// TODO Auto-generated method stub
		return adminForm;
	}

	public void getUser(){
		HashMap result = new HashMap();
		try {
			User user = adminBusiness.getUser(adminForm);
			result.put("username", user.getUsername());
			result.put("password", user.getPassword());
			result.put("appURL", user.getAppURL());
			result.put("queryRight", user.getQueryRight());
			JsonUtil.output(result);
		} catch (Exception e) {
			// TODO: handle exception
			result = new HashMap();
			result.put("error", -1);
			result.put("message", "unknowError");
			JsonUtil.output(result);
		}
	}

	public void listUsers() {
		HashMap result = new HashMap();
		if(adminForm.getKeyType() != null && 
				!adminForm.getKeyType().equals("username") && 
				!adminForm.getKeyType().equals("appURL")){
			result.put("error", -1);
			result.put("message", "keyType Error");
			JsonUtil.output(result);
		}
		try {
			List<User> list = adminBusiness.listUsers(adminForm.isCqr(), adminForm.getKey(), 
					adminForm.getKeyType() ,adminForm.getPageIndex(), adminForm.getPageSize());
			List data = new LinkedList();
			for(User user:list){
				Map map = new HashMap();
				map.put("username", user.getUsername());
				map.put("password", user.getPassword());
				map.put("appURL", user.getAppURL());
				map.put("queryRight", user.getQueryRight());
				data.add(map);
			}
			result.put("data",data);
			result.put("total", adminBusiness.getCount(adminForm.isCqr(), adminForm.getKey(), 
					adminForm.getKeyType()) );
			//result.put("pageIndex", pageIndex);
			result.put("result", "success");
		} catch (Exception e) {
			result = new HashMap();
			result.put("error", -1);
			result.put("message", "unknowError");
		} finally {
			JsonUtil.output(result);
		}
	}

	public void login() {
		Result result = null;
		String s = ServletActionContext.getRequest().getParameter("useCookie");
		try {
			result = adminBusiness.Login(adminForm);
		} catch (Exception e) {
			//e.printStackTrace();
			result = Result.unknownError();
		} finally {
			JsonUtil.output(result);
		}
	}

	public String logout() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		response.setHeader("Pragma", "No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expire", 0);
		adminBusiness.logout();
		return "logout";
	}


	public void regist() {
		Result result = null;
		try {
			result = adminBusiness.regist(adminForm);
		} catch (Exception e) {
			//e.printStackTrace();
			result = Result.unknownError();
		} finally {
			JsonUtil.output(result);
		}
	}

	public void updateUser(){
		Result result = null;
		try {
			String json = ServletActionContext.getRequest().getParameter("data");
			User user = JsonUtil.toBean(json,User.class);
			result = adminBusiness.userUpdate(user);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result = Result.unknownError();
		}finally{
			JsonUtil.output(result);
		}
	}
}